<?php 
require_once("base.php");
class admin extends base {
    
    public function __construct() {
        @session_start();
        parent::__construct();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->model('adminmodel');
        $this->load->database();
    }
    
    public function index() {
        $data['webb'] = $this->webbPath();
        $this->adminHeader();
        $this->load->view('admin/index', $data);
        $this->adminFooter();
    }
    
    public function addmenu() {
        $data['query'] = $this->adminmodel->getMenu();
        $data['webb'] = $this->webbPath();
        $this->load->view('admin/addmenu', $data);
    }
    
    public function addPage() {
        $data['webb'] = $this->webbPath();
        $data['query'] = $this->adminmodel->getMenu();
        $this->load->view('admin/addPage', $data);
    }
    
    public function appendMenu() {
        $name = $this->input->post('label');
        $parent = $this->input->post('parent');
        
        $this->adminmodel->injectMenu($name, $parent);
    }
    
    public function appendPage() {
        $name = $this->input->post('title');
        $parent = $this->input->post('parent');
        
        $this->adminmodel->injectPage($name, $parent);
    }
    
    public function managePages() {
        $data['webb'] = $this->webbPath();
        $this->load->view('admin/managePages', $data);
    }


    public function loginView() {
        $data['webb'] = $this->webbPath();
        $this->load->view('admin/login', $data);
    }
    
    public function login() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $checked  = $this->input->post('checked');
        $token    = $this->input->post('token');
        
        $login = $this->adminmodel->verifyUser($username, $password);
        if($login !== false) {
            foreach($login as $key => $val) {
                if($key == "password") {
                    // do nothing
                }
                else {
                    $_SESSION[$key] = $val;
                    if($checked == true) {
                        if($key == "username" || $key == "email") {
                            setcookie($key, $val, time() + 60 + 60 +24 +30);
                        }
                    }
                }
            }
        }
    }
    
    public function logout() {
        setcookie('username', '', time() - 3600);
        setcookie('email', '', time() - 3600);
        session_destroy();
    }
    
}